<template>
	<tm-fullView bg-color="#ffffff">
		<tm-menubars v-if="step==1" :title="$t('米聊')" iconColor="white" color="#ffffff" :showback="false">
			<view slot="right" style="padding-right: 20rpx;">
				<tm-icons @click="show=true" size="40" name="fa-language"></tm-icons>
			</view>
		</tm-menubars>
		<view class="login__wrapper" v-if="step==1">
			<view class="title">{{$t('验证码登录')}}</view>
			<view class="sub-title">{{$t('首次登录自动注册')}}</view>
			<view class="login-info">
				<tm-input :round="20" v-model="email"  :placeholder="$t('请输入你的邮箱')" :border-bottom="false"  bg-color="grey-lighten-3" border-color="grey-lighten-3"></tm-input>
				<view class="send">
					<tm-button :round="24" block @click="sendCode">{{$t('获取验证码')}}</tm-button>
				</view>
				<view class="agreement">
					<tm-checkbox v-model="checked" :dense="true" :size="24"></tm-checkbox>
					<text class="text">{{$t('已阅读并同意')}}</text>
					<text class="link">{{$t('用户协议')}}</text>
					<text class="text">{{$t('和')}}</text>
					<text class="link">{{$t('隐私协议')}}</text>
				</view>
			</view>
		</view>
		<tm-menubars v-if="step==2" :title="$t('输入验证码')" iconColor="white" color="#ffffff" :showback="false"></tm-menubars>
		<view class="login__wrapper" v-if="step==2">
			<view class="title">{{$t('验证码已发送')}}</view>
			<view class="sub-title">{{$t('我们已给你的邮箱发送了一个4位二维码，输入验证码登录即可')}}</view>
			<view class="verify-info">
				<view class="tips">
					{{$t('为了确保本人操作，请输入验证码')}}
				</view>
				<view class="verify-code">
					<tm-password @confirm="login"  v-model="verifyCode" :code-length="4" :show-val="true" noval="mline"></tm-password>
				</view>
				<view class="send">
					<tm-button :round="24" block @click="login">{{$t('登录')}}</tm-button>
					<view class="resend">
						<text>{{$t('重新获取')}}{{time}}s</text>
					</view>
				</view>
			</view>
		</view>
		<tm-actionSheetMenu :title="$t('请选择语言')"  @change="changeLang" v-model="show" :actions="langs"></tm-actionSheetMenu> 
	</tm-fullView>
</template>

<script>
	import IndexApi from '@/api/index.js'
import sleep from '../../tm-vuetify/tool/function/sleep'
	export default {
		data() {
			return {
				checked:false,
				step:1,
				verifyCode:'',
				email:'2518687673@qq.com',
				time:120,
				timer:null,
				show:false,
				defaultLang:''
			}
		},
		computed:{
			langs(){
				var langList=this.$tm.vx.state().app.config.langList
				let _langs=[]
				if(langList){
					langList.forEach(item=>{
						_langs.push(item.name)
					})
					return _langs
				}
				return _langs
			},
		},
		methods: {
			sendCode(){
				let self=this
				if(this.email==''){
					uni.showToast({
						title:self.$t('请输入你的邮箱'),
						icon:'none'
					})
					return
				}
				if(!this.checked){
					uni.showToast({
						title:self.$t('请阅读并同意用户协议和隐私协议'),
						icon:'none'
					})
					return
				}
				uni.showLoading({
					title:self.$t('正在获取验证码')
				})
				IndexApi.sendMailCode({email:this.email}).then(res=>{
					uni.hideLoading()
					uni.showToast({
						title:res.msg,
						icon:'none'
					})
					if(res.code==200){
						this.step=2
						self.timer = setInterval(function() {
							if (self.time == 0) {
								clearInterval(self.timer);
								self.time=120
							} else {
								self.time--;
							}
						}, 1000);
						
					}
				})
			},
			login(){
				let self=this
				if(this.email==''){
					uni.showToast({
						title:self.$t('请输入你的邮箱'),
						icon:'none'
					})
					return
				}
				if(!this.checked){
					uni.showToast({
						title:self.$t('请阅读并同意用户协议和隐私协议'),
						icon:'none'
					})
					return
				}
				if(this.verifyCode==''){
					uni.showToast({
						title:self.$t('请输入验证码'),
						icon:'none'
					})
					return
				}
				uni.showLoading({
					title:self.$t('登录中')
				})
				IndexApi.loginByCode({email:self.email,code:self.verifyCode}).then(res=>{
					uni.hideLoading()
					uni.showToast({
						title:res.msg,
						icon:'none'
					})
					if(res.code==200){
						uni.setStorageSync("token",res.data)
						self.$tm.vx.actions('user/getUserInfo')
						uni.switchTab({
							url:'/pages/index/index'
						})
					}
				})
			},
			async changeLang(e){
				let self=this
				self.show=false
				uni.showLoading({
					title:self.$t('正在加载语言')
				})
				var lang=self.$tm.vx.state().app.config.langList[e.index]
				uni.setStorageSync("lang",lang.varName)
				self.$tm.vx.commit('translate/setDefaultLang',lang.varName)
				//初始化翻译
				var res=await self.$tm.vx.actions('translate/getTranslateList')
				uni.setStorageSync('translateList',res)
				setTimeout(()=>{
					uni.hideLoading()
					uni.showToast({
						title:self.$t('语言切换成功')
					})
					self.setTabbar()
					self.defaultLang=lang.name
				},1000)
				
			},
		}
	}
</script>

<style lang="scss" scoped>
.login__wrapper{
	padding-top: 80rpx;
	.title{
		margin: 0 30rpx;
		font-weight: 700;
		font-size: 50rpx;
	}
	.sub-title{
		color: #999999!important;
		font-size: 32rpx;
		margin: 30rpx;
	}
	.login-info{
		margin-top: 100rpx;
		.send{
			padding: 30rpx;
		}
		.agreement{
			margin-top: 20rpx;
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: center;
			.text{
				font-size: 24rpx;
			}
			.link{
				font-size: 24rpx;
				color: #2196F3;
				text-decoration: underline
			}
		}
	}
	.verify-info{
		.tips{
			font-size: 32rpx;
			text-align: center;
		}
		margin-top: 100rpx;
		.verify-code{
			margin-top: 40rpx;
			padding: 20rpx 30rpx;
		}
		.send{
			padding: 30rpx;
			.resend{
				padding: 30rpx;
				text-align: right;
				color: #2196F3;
				font-size: 28rpx;
			}
		}
	}
}

</style>
